<?php

class Application_Model_ProjectMapper extends Ahs_Model_Mapper
{
    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_Projects';
    }
    
    public function fetchByCreatorId($creatorid)
    {
        
        $rows = $this->getDbTable()->fetchAll(array('project_creator = ?' => $creatorid,'project_deleted = ?'=>false));
        return $this->_toProjects($rows);
        
    }
    
    
    public function fetchByUserId($userId)
    {
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $sql = $db->select()->from('projects')->join('projects_has_users', 'projects_project_id = project_id')->where('users_user_id = ?', $userId);
        $rows = $db->fetchAll($sql);
        return $this->_toProjects($rows);
    }
    
    public function fetchUsersFromProjectId($projectId){
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $sql = $db->select()->from('projects_has_users')->join('users', 'user_id = users_user_id')->where('projects_project_id = ?', $projectId);
        $rows = $db->fetchAll($sql);
        return $rows;
    }
    
    public function doesUserOwnsProject($userid, $projectid){
        $count = $this->getDbTable()->fetchAll(array('project_creator = ?' => $userid, 'project_id = ?' => $projectid))->count();
        
        if($count > 0)
            return true;
    }
    public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array('project_id = ?' => $id));
        return $this->_toProject($row);
    }

    public function fetchAll()
    {        
        $rows = $this->getDbTable()->fetchAll();

        return $this->_toProjects($rows);
    }
    
    private function _toProject($row = null)
    {
        $project = new Application_Model_Project();
        $project->Id            = $row['project_id'];
        $project->Name          = $row['project_name'];
        $project->Description   = $row['project_description'];
        $project->Onhold        = $row['project_onhold'];
        $project->Deleted       = $row['project_deleted'];
        $project->Starttime     = $row['project_starttime'];
        $project->Endtime       = $row['project_endtime'];
        $project->Creationdate  = $row['project_creationdate'];
        $project->Creator       = $row['project_creator'];
        
        return $project;
        
        
    }
    private function _toProjects($rows = null)
    {
        $projects = array();
        
        foreach($rows as $row)
        {
            $projects[] = $this->_toProject($row);
        }
        
        return $projects;
    }

    public function delete($projectid){
        $data = array('project_deleted' => true);
        $row = $this->getDbTable()->update($data, array('project_id = ?'=>$projectid));
        if($row > 0){
            return true;
        }
        return false;
         
    }
    public function save(Application_Model_Project $project)
    {
        $data = array(
            'project_name'          => $project->Name,
            'project_description'   => $project->Description,
            'project_creator'       => $project->Creator,
            'project_starttime'     => $project->Starttime,
            'project_endtime'       => $project->Endtime,
        );
        
        
        if($project->Id === null){
            $this->getDbTable()->insert($data);
        }
        else
        {
            $this->getDbTable()->update($data,array('project_id = ?' => $project->Id));
        }
        
    }

}